(function(){
var wid=$('#svgbox').width(),
	hei=$('#svgbox').height(),
	self=this;
var shuxianone,
	shuxiantwo,
	danxian,
	henxian=hei*0.7/6,
	orq=hei*0.7,
	orl=hei*0.3,
	orlline=hei*0.3/2,
	linewid,
	klinewid;
var data=[];
defalut(1);

function defalut(el){
	var timege=420/el,
		timea=135/el,
		timeb=225/el;
	klinewid=1380/el;
	shuxianone=wid/timege*timea;
	shuxiantwo=wid/timege*timeb;
	danxian=wid/timege;
	linewid=wid/timege/2;
	
	svgajax(el);
}
function svgajax(el){
	ajaxsd(el)
	function ajaxsd(el){
		var urlchange;
		if(el=1){
			urlchange=urls
		}else if(5){
			urlchange=urlfive
		}
		$.ajax({
		type:"get",
		url:urlchange,
		dataType:'json',
		async:true,
		success:function(datasd){
			data=datasd;
			init();
		},
		error:function(e){
			console.log(e)
		}
		});
	};
	setInterval(ajaxsd(el),60000);
}

function makeSVG(tag, attrs) {
    var el= document.createElementNS('http://www.w3.org/2000/svg', tag);
    for (var k in attrs)
        el.setAttribute(k, attrs[k]);
    return el;
}
function init(){
	dirdata();
	picdark();
	linedark();
	circle();
}
var dsvg=$('#svgbox').find('g').eq(0);
bakc()
function bakc(){
	var shulineone=makeSVG('line', {x1: wid*0.2608, x2: wid*0.2608, y1:0, y2:orq, stroke: '#f8f8f8'});//12.00
	dsvg.append(shulineone);
	var shulinetwo=makeSVG('line', {x1: wid*0.7826, x2: wid*0.7826, y1:0, y2:orq, stroke: '#f8f8f8'})//24.00
	dsvg.append(shulinetwo);
	for(var i=0;i<6;i++){
		var henline=makeSVG('line', {x1: 0, x2: wid, y1:henxian*(i+1), y2:henxian*(i+1), stroke: '#f8f8f8'});
		dsvg.append(henline);
	}
	dsvg.find('text').eq(0).attr('y',hei*0.7+14);//06.00
	dsvg.find('text').eq(1).attr({'x':wid-30,'y':hei*0.7+14});//05.00
	dsvg.find('text').eq(2).attr({'x':wid*0.2608-13,'y':hei*0.7+14});//12.00
	dsvg.find('text').eq(3).attr({'x':wid*0.7826-13,'y':hei*0.7+14});//24.00
};

var tu,fenxian,baifen,jiange;
function dirdata(){
	tu=data['tu'];
	fenxian=data['shi'];
	jiange=(fenxian.hign-fenxian.low)*100;
	baifen=hei*0.7/jiange;
	dsvg.find('text').eq(4).find('tspan').attr({'y':"20",'x':wid-50}).html(fenxian.hign);
	dsvg.find('text').eq(5).find('tspan').attr({'y':hei*0.7-10,'x':wid-50}).html(fenxian.low);
	
	var stappp=hei*0.7-(baifen*(fenxian.open-fenxian.low)*100);
	dsvg.find('text').eq(6).find('tspan').attr({'y':stappp-5,'x':20}).html(fenxian.open);
	dsvg.find('line').eq(0).attr({'x1':"0",'x2':wid,'y1':stappp,'y2':stappp});
//	console.log(jiange)
}
var osvg=$('#svgbox').find('g').eq(1),
	osvgline=osvg.find('path').eq(1),
	osvgpic=osvg.find('path').eq(0),
	last;
function picdark(){	
	var start=baifen*(tu[0].ys-fenxian.low)*100,
		lineone="M 0,"+(hei*0.7-start);
	$.map(data['tu'], function(val,i) {
		var bod=(val.ys-fenxian.low)*baifen*100;
		lineone+="L"+(i+1)*wid/klinewid+","+(hei*0.7-bod);
	});
	lineone=lineone+"L"+data['tu'].length*wid/klinewid+","+hei*0.7 +"L"+0+","+hei*0.7 +"Z";
	
	var linetwo="M 0,"+(hei*0.7-start);
	$.map(data['tu'], function(val,i) {
		var bod=(tu[i].ys-fenxian.low)*baifen*100;
		last=bod;
		linetwo=linetwo+"L"+(i+1)*wid/klinewid+","+(hei*0.7-bod);
	});
	osvgline.attr('d',linetwo);
	osvgpic.attr('d',lineone);
};
var linesvg=$('#svgbox').find('g').eq(2);
function linedark(){
	linesvg.find('line').remove();
	var htmls='',max=0;
	$.map(data['tu'], function(val,i) {
		if(val.zs>max){
			max=val.zs
		};
	});
	max+=100;
	$.map(data['tu'], function(val,i) {
		var henline=makeSVG('line', {x1:i*wid/klinewid, x2: i*wid/klinewid, y1:hei, y2:hei-(hei*0.3/max*tu[i].zs), stroke: '#d0402d','stroke-width':linewid});
		linesvg.append(henline);
	});
};
function circle(){
	osvg.find('circle').remove();
	var circle= makeSVG('circle', {cx: data['tu'].length*wid/klinewid, cy: (hei*0.7-last), r:2, stroke: 'none', 'stroke-width': "none", fill: 'red'});
	osvg.append(circle);
}
})()
